<?

//*********************************************
// NOMBRE DE LAS TABLAS
//*********************************************
$select_tables_name      = "SELECT * FROM SYSOBJECTS WHERE name LIKE 'cat%' AND name NOT LIKE 'cat_EncuestaStatus' AND name NOT LIKE 'cat_EncuestasTipoSeleccion'";
$rselect_tables_name     = mssql_query($select_tables_name);

if($catplgxEnable == 0) {
    $enabled_cat = "readonly disabled";
}else{
    $enabled_cat = "";
}


if ($rowrselect_tables_name = mssql_fetch_array($rselect_tables_name)){
    $tableNameList  = "<select name=\"cat_table\" onChange=\"Enviar2();\" ".$enabled_cat.">\n";
    $tableNameList .=  "<option value=\"0\">Selecciona el cat&aacute;logo ----></option>\n";
    
    do {

        if($_POST['cat_table'] == $rowrselect_tables_name['name']) {
            $selectedtable = "selected";
        }else{
            $selectedtable = "";
        }

        $tableNameList .=  "<option value=\"".$rowrselect_tables_name['name']."\" ".$selectedtable.">".$rowrselect_tables_name['name']."\n";
        
    }while ($rowrselect_tables_name = mssql_fetch_array($rselect_tables_name));

    $tableNameList .= "</select>\n";

}else{
    $tableNameList = "<select name=\"\" disabled readonly>\n";
    $tableNameList .=  "<option value=\"0\">No hay cat&aacute;logos</option>\n";
    
}

$tableNameList .= "</select>\n";

if($proceso == 2) {

    $nombres_campos = "SELECT * FROM ".$_POST['cat_table'];
    $rnombres_campos     = mssql_query($nombres_campos);


    if ($fld = mssql_fetch_field($rnombres_campos)){

        $tableCatalogIDx  = "<select name=\"cat_id\" ".$enabled_cat.">\n";
        $tableCatalogIDx .=  "<option value=\"0\">Selecciona el campo ----></option>\n";
        $tableCatalogTextox  = "<select name=\"cat_txt\" ".$enabled_cat.">\n";
        $tableCatalogTextox .=  "<option value=\"0\">Selecciona el campo ----></option>\n";

        do {

            $tableCatalogIDx .=  "<option value=\"".$fld->name."\">".$fld->name."\n";
            $tableCatalogTextox .=  "<option value=\"".$fld->name."\">".$fld->name."\n";

        }while($fld = mssql_fetch_field($rnombres_campos));

        $tableCatalogIDx .= "</select>\n";
        $tableCatalogTextox .= "</select>\n";

    }else{
        $tableCatalogIDx = "<select name=\"cat_id\" disabled readonly>\n";
        $tableCatalogIDx .=  "<option value=\"0\">No hay campos en la tabla</option>\n";
        $tableCatalogTextox = "<select name=\"cat_txt\" disabled readonly>\n";
        $tableCatalogTextox .=  "<option value=\"0\">No hay campos en la tabla</option>\n";

    }

}

if($catplgxStartResult == 1) {

$string_select = $row_01['config_Pregunta'];

$one_step = explode("}",$string_select); // {xxxx}{yyyy}{wwww}
$datos_catalogo = array();
for($hx=0;$hx<5;$hx++) {
$datos_catalogo[$hx] = str_replace("{","",$one_step[$hx]);
}

if((int) $datos_catalogo[3]==0) {
  $no_nueves = " where ".$datos_catalogo[1]." != 9999 ";
}else{
  $no_nueves = "";
}

$select_select_cat   = "SELECT * FROM ".$datos_catalogo[0].$no_nueves;
$rselect_select_cat  = mssql_query($select_select_cat);

if ($rowrrselect_select_cat = mssql_fetch_array($rselect_select_cat)){

$select_cat_finished = "";

if($datos_catalogo[4] == "COMBO"){

    // ********** SI ES OBLIGATORIA *****************
    if($start_cat_valida == 1) {
        $JavaIA_check .= "if((forma.pregunta_cat".$row_01['id_EncuestaPregunta'].".selectedIndex == 0) && (error == 0)) {
        fixElement(forma.pregunta_cat".$row_01['id_EncuestaPregunta'].", \"Por favor, introduzca su respuesta de la pregunta ".$_number.".\");
        error = 1;
        }";
    }
    // ********** END SI ES OBLIGATORIA *************

    $select_cat_finished .= "<select name=\"pregunta_cat".$row_01['id_EncuestaPregunta']."\" ".$enabled_cat.">\n";
    $select_cat_finished .=  "<option value=\"0\">Selecciona ----></option>\n";

    do {

        $select_cat_finished .=  "<option value=\"".$rowrrselect_select_cat[$datos_catalogo[1]]."\">".$rowrrselect_select_cat[$datos_catalogo[2]]."\n";

    }while ($rowrrselect_select_cat = mssql_fetch_array($rselect_select_cat));

    $select_cat_finished .= "</select>\n";
  
}else if($datos_catalogo[4] == "RADIO"){

    // ********** SI ES OBLIGATORIA *****************
    if($start_cat_valida == 1) {
        $JavaIA_check .= "if ((!(";
    }
    // ********** END SI ES OBLIGATORIA *************
    $_mnb = 0;
    do {

        // ********** SI ES OBLIGATORIA *****************
        if($start_cat_valida == 1) {
            if($_mnb == 0) {
                $JavaIA_check .= "forma.pregunta_cat".$row_01['id_EncuestaPregunta']."[".$_mnb."].checked ";
            }else{
                $JavaIA_check .= "|| forma.pregunta_cat".$row_01['id_EncuestaPregunta']."[".$_mnb."].checked ";
            }
        }
        // ********** END SI ES OBLIGATORIA *************
    
    $select_cat_finished .=  "<input type=\"radio\" name=\"pregunta_cat".$row_01['id_EncuestaPregunta']."\" value=\"".$rowrrselect_select_cat[$datos_catalogo[1]]."\" ".$enabled_cat.">".$rowrrselect_select_cat[$datos_catalogo[2]]."<br>\n";
        
    $_mnb++;
    }while ($rowrrselect_select_cat = mssql_fetch_array($rselect_select_cat));
    
    // ********** SI ES OBLIGATORIA *****************
    if($start_cat_valida == 1) {
        $JavaIA_check .= ")) && (error == 0)) {
        alert(\"Por favor, introduzca su respuesta de la pregunta ".$_number.".\");
        error = 1;
        }\n";
    }
    // ********** END SI ES OBLIGATORIA *************
  
}else if($datos_catalogo[4] == "CHECKBOX"){

    // ********** SI ES OBLIGATORIA *****************
    if($start_cat_valida == 1) {
        $JavaIA_check .= "if(";
    }
    // ********** END SI ES OBLIGATORIA *************

    $_y = 0;
    do {

        // ********** SI ES OBLIGATORIA *****************
        if($start_cat_valida == 1) {
            if($_y == 0) {
                $JavaIA_check .= "(forma.pregunta_cat".$row_01['id_EncuestaPregunta']."_".$_y.".checked == false)";
            }else{
                $JavaIA_check .= " && (forma.pregunta_cat".$row_01['id_EncuestaPregunta']."_".$_y.".checked == false)";
            }
        }
        // ********** END SI ES OBLIGATORIA *************

        $select_cat_finished .=  "<input type=\"checkbox\" name=\"pregunta_cat".$row_01['id_EncuestaPregunta']."_".$_y."\" value=\"".$rowrrselect_select_cat[$datos_catalogo[1]]."\" ".$enabled_cat.">".$rowrrselect_select_cat[$datos_catalogo[2]]."<br>\n";

    $_y++;
    }while ($rowrrselect_select_cat = mssql_fetch_array($rselect_select_cat));

    // ********** SI ES OBLIGATORIA *****************
    if($start_cat_valida == 1) {
        $JavaIA_check .= " && (error == 0)) {
        alert(\"Por favor, introduzca su respuesta de la pregunta ".$_number.".\");
        error = 1;
        }\n";
    }
    // ********** END SI ES OBLIGATORIA *************
  
}

}else{
    $select_cat_finished = "<select name=\"\" disabled readonly>\n";
    $select_cat_finished .=  "<option value=\"0\">No hay resultados</option>\n";
    $select_cat_finished .= "</select>\n";
}

}


